import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";

Page({
  data: {
    hasToken: false,
    avatarUrl: "",
    name: "",
  },

  async onLoad() {
    const token = getApp().token;
    if (token) this.getUserInfo(token);
    this.setData({
      hasToken: !!token,
    });
  },

  async onShow() {
    await getApp().setTabBar();
  },

  async getUserInfo(token) {
    if (!getApp().token) return wx.utils.toast("请先登录");
    const res = await wx.http.get("/v1_0/user/profile", token);
    console.log("用户个人信息：", res);
    if (res.statusCode === 200) {
      getApp().userInfo = res.data.data;
      console.log("仓库用户信息", getApp().userInfo);
      this.setData({
        avatarUrl: res.data.data.photo,
        name: res.data.data.name || res.data.data.mobile,
      });
    }
  },

  goEditProfile() {
    if (getApp().token) {
      wx.navigateTo({
        url: "/pages/profile/index",
      });
    } else {
      wx.utils.toast("请先登录");
    }
  },
  logout() {
    Dialog.confirm({
      title: "黑马头条",
      message: "是否确认退出该账号？",
    })
      .then(async () => {
        wx.removeStorageSync("HMTT-token");
        getApp().token = "";
        wx.utils.toast("退出成功！");
        console.log(getApp().token);
        await new Promise((resolve, reject) => {
          setTimeout(function () {
            resolve();
          }, 800);
        });
        wx.redirectTo({
          url: "/pages/index/index",
        });
      })
      .catch((err) => {});
  },
});
